package com.tjsinfo.tjsoa.doc.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.jbpm.taskmgmt.exe.TaskInstance;

import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.doc.bo.ITDocDocBO;
import com.tjsinfo.tjsoa.doc.bo.ITDocDocOpeBO;
import com.tjsinfo.tjsoa.doc.vo.TDocDoc;
import com.tjsinfo.tjsoa.doc.vo.TDocDocOpe;
import com.tjsinfo.tjsoa.workflow.WorkFlowTemplate;

public class DocCheckAction extends DispatchAction {
	private ITDocDocBO docBo;
	private ITDocDocOpeBO docOpeBo;
	
	public void setDocBo(ITDocDocBO docBo) {
		this.docBo = docBo;
	}
	public void setDocOpeBo(ITDocDocOpeBO docOpeBo) {
		this.docOpeBo = docOpeBo;
	}	
	
	private void docOpe(HttpServletRequest request,WorkFlowTemplate test,String docId,Long taskid,Integer docMark) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		String feedBack = request.getParameter("feedBack");
		Integer checkId = ToolKit.getSessionUser(request).getUser_id();
		String checkName = ToolKit.getSessionUser(request).getUsername();

		TDocDocOpe tddo = new TDocDocOpe();
		tddo.setDocId(docId);
		tddo.setDateTime(sdf.parse(sdf.format(date)));
		tddo.setFeedBack(feedBack);
		tddo.setCheckId(checkId.toString());
		tddo.setCheckName(checkName);
		tddo.setTaskid(Integer.parseInt(taskid.toString()));
		TaskInstance taskInstance = test.nextStep(taskid);
		String node = taskInstance.getName();
		tddo.setNode(node);
		tddo.setMark(docMark);
		docOpeBo.save(tddo);
	}
	
	//原件
	public ActionForward oriCheck(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date1 = new Date();
			String docId = request.getParameter("docId");
			Long taskid = Long.valueOf(request.getParameter("taskid"));
			Integer userId = ToolKit.getSessionUser(request).getUser_id();
			String userName = ToolKit.getSessionUser(request).getUsername();
			String linkurl = "doc/ori/oriCheckList.jsp?method=checkList";
			String message = "doc.oriCheck.message";
			
			WorkFlowTemplate test = new WorkFlowTemplate();
			docOpe(request,test,docId,taskid,0);
			
			TaskInstance taskInstance = test.nextStep(taskid);
			String node1 = taskInstance.getName();
			TDocDoc tdd = docBo.findById(docId);
			
			tdd.setUserId(tdd.getUserId()+userId+",");
			if("办公室登记".equals(node1)){
				tdd.setStatus("借出");
				tdd.setOutTime(sdf1.parse(sdf1.format(date1)));
				tdd.setSendId(userId.toString());
				tdd.setSendName(userName);
			}else if("归还".equals(node1)){
				tdd.setStatus("归还");
				tdd.setBackTime(sdf1.parse(sdf1.format(date1)));
			}else if("申请人修改".equals(node1)){
				String docName = request.getParameter("docName");
				String docUse = request.getParameter("docUse");
				String remarks = request.getParameter("remarks");
				
				tdd.setDateTime(sdf1.parse(sdf1.format(date1)));
				tdd.setDocName(docName);
				tdd.setDocUse(docUse);
				tdd.setRemarks(remarks);
				tdd.setStatus("审核中");
				linkurl = "doc/ori/oriList.jsp";
				message = "doc.oriAdd.message";				
			}
			
			docBo.update(tdd);
			
			test.nextStep(taskid,null);	
			
			request.setAttribute("linkurl", linkurl);
			request.setAttribute("message", message);	
			
			return mapping.findForward("success");
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("failed");
		}
	}
	
	public ActionForward oriReturnCheck(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			String docId = request.getParameter("docId");
			Long taskid = Long.valueOf(request.getParameter("taskid"));
			
			WorkFlowTemplate test = new WorkFlowTemplate();
			docOpe(request,test,docId,taskid,0);
			
			TDocDoc tdd = docBo.findById(docId);
			tdd.setUserId(tdd.getUserId()+ToolKit.getSessionUser(request).getUser_id()+",");
			tdd.setStatus("退回");
			docBo.update(tdd);
			
			Long flowid = Long.parseLong(tdd.getFlowid().toString());
			test.setVariable("申请人修改",tdd.getApplyId(), flowid);
			test.nextStep(taskid,"to 申请人修改");	
			
			request.setAttribute("linkurl", "doc/ori/oriCheckList.jsp?method=checkList");
			request.setAttribute("message", "doc.oriCheck.message");	
			
			return mapping.findForward("success");
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("failed");
		}
	}	
	
	//证件
	public ActionForward cerCheck(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date1 = new Date();
			String docId = request.getParameter("docId");
			Long taskid = Long.valueOf(request.getParameter("taskid"));
			Integer userId = ToolKit.getSessionUser(request).getUser_id();
			String userName = ToolKit.getSessionUser(request).getUsername();
			String linkurl = "doc/cer/cerCheckList.jsp?method=checkList";
			String message = "doc.cerCheck.message";
			
			WorkFlowTemplate test = new WorkFlowTemplate();
			docOpe(request,test,docId,taskid,1);
			
			TaskInstance taskInstance = test.nextStep(taskid);
			String node1 = taskInstance.getName();
			TDocDoc tdd = docBo.findById(docId);
			tdd.setUserId(tdd.getUserId()+userId+",");
			
			if("办公室主管审批".equals(node1)){
				String verify = request.getParameter("verify");
				String office1Id = request.getParameter("office1Id");				
				Long flowid = Long.parseLong(tdd.getFlowid().toString());
				if("1".equals(verify)){
					test.setVariable("var1","N", flowid);
					test.setVariable("办公室主任审批",office1Id, flowid);
				}else{
					test.setVariable("var1","back", flowid);
					tdd.setStatus("借出");
					tdd.setOutTime(sdf1.parse(sdf1.format(date1)));
				}
				tdd.setSendId(userId.toString());
				tdd.setSendName(userName);				
			}else if("办公室主任审批".equals(node1)){
				tdd.setStatus("借出");
				tdd.setOutTime(sdf1.parse(sdf1.format(date1)));
			}else if("归还".equals(node1)){
				tdd.setStatus("归还");
				tdd.setBackTime(sdf1.parse(sdf1.format(date1)));
			}else if("申请人修改".equals(node1)){
				String docName = request.getParameter("docName");
				String docUse = request.getParameter("docUse");
				String remarks = request.getParameter("remarks");
				
				tdd.setDateTime(sdf1.parse(sdf1.format(date1)));
				tdd.setDocName(docName);
				tdd.setDocUse(docUse);
				tdd.setRemarks(remarks);
				tdd.setStatus("审核中");
				linkurl = "doc/ori/oriList.jsp";
				message = "doc.oriAdd.message";				
			}
			
			docBo.update(tdd);
			
			test.nextStep(taskid,null);	
			
			request.setAttribute("linkurl", linkurl);
			request.setAttribute("message", message);	
			
			return mapping.findForward("success");
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("failed");
		}
	}
	
	public ActionForward cerReturnCheck(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			String docId = request.getParameter("docId");
			Long taskid = Long.valueOf(request.getParameter("taskid"));
			
			WorkFlowTemplate test = new WorkFlowTemplate();
			docOpe(request,test,docId,taskid,1);
			
			TDocDoc tdd = docBo.findById(docId);
			tdd.setUserId(tdd.getUserId()+ToolKit.getSessionUser(request).getUser_id()+",");
			tdd.setStatus("退回");
			docBo.update(tdd);
			
			Long flowid = Long.parseLong(tdd.getFlowid().toString());
			test.setVariable("申请人修改",tdd.getApplyId(), flowid);
			test.nextStep(taskid,"to 申请人修改");	
			
			request.setAttribute("linkurl", "doc/cer/cerCheckList.jsp?method=checkList");
			request.setAttribute("message", "doc.cerCheck.message");	
			
			return mapping.findForward("success");
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("failed");
		}
	}
	
	//复印件

	public ActionForward copyCheck(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date1 = new Date();			
			
			String docId = request.getParameter("docId");
			Long taskid = Long.valueOf(request.getParameter("taskid"));
			Integer userId = ToolKit.getSessionUser(request).getUser_id();
			String userName = ToolKit.getSessionUser(request).getUsername();
			String linkurl = "doc/copy/copyCheckList.jsp?method=checkList";
			String message = "doc.copyCheck.message";
			String nextStep = null;
			
			WorkFlowTemplate test = new WorkFlowTemplate();
			docOpe(request,test,docId,taskid,2);
			
			TaskInstance taskInstance = test.nextStep(taskid);
			String node1 = taskInstance.getName();
			TDocDoc tdd = docBo.findById(docId);
			tdd.setUserId(tdd.getUserId()+userId+",");
			if("办公室主管审批".equals(node1)){
				String verify = request.getParameter("verify");
				String office1Id = request.getParameter("office1Id");				
				Long flowid = Long.parseLong(tdd.getFlowid().toString());
				if("1".equals(verify)){
					test.setVariable("var1","N", flowid);
					test.setVariable("办公室主任审批",office1Id, flowid);
				}else{
					test.setVariable("var1","Y", flowid);
					tdd.setStatus("借出");
					tdd.setOutTime(sdf1.parse(sdf1.format(date1)));
				}
				tdd.setSendId(userId.toString());
				tdd.setSendName(userName);				
			}else if("办公室主任审批".equals(node1)){
				tdd.setStatus("借出");
				tdd.setOutTime(sdf1.parse(sdf1.format(date1)));
				nextStep = "to 结束";
			}else if("申请人修改".equals(node1)){
				String docName = request.getParameter("docName");
				String docUse = request.getParameter("docUse");
				String remarks = request.getParameter("remarks");
				
				tdd.setDateTime(sdf1.parse(sdf1.format(date1)));
				tdd.setDocName(docName);
				tdd.setDocUse(docUse);
				tdd.setRemarks(remarks);
				tdd.setStatus("审核中");
				linkurl = "doc/ori/oriList.jsp";
				message = "doc.oriAdd.message";				
			}
			
			docBo.update(tdd);
			
			test.nextStep(taskid,null);	
			
			request.setAttribute("linkurl", linkurl);
			request.setAttribute("message", message);	
			
			return mapping.findForward("success");
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("failed");
		}
	}
	
	public ActionForward copyReturnCheck(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			String docId = request.getParameter("docId");
			Long taskid = Long.valueOf(request.getParameter("taskid"));
			
			WorkFlowTemplate test = new WorkFlowTemplate();
			docOpe(request,test,docId,taskid,2);
			
			TDocDoc tdd = docBo.findById(docId);
			tdd.setUserId(tdd.getUserId()+ToolKit.getSessionUser(request).getUser_id()+",");
			tdd.setStatus("退回");
			docBo.update(tdd);
			
			Long flowid = Long.parseLong(tdd.getFlowid().toString());
			test.setVariable("申请人修改",tdd.getApplyId(), flowid);
			test.nextStep(taskid,"to 申请人修改");	
			
			request.setAttribute("linkurl", "doc/copy/copyCheckList.jsp?method=checkList");
			request.setAttribute("message", "doc.copyCheck.message");	
			
			return mapping.findForward("success");
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("failed");
		}
	}		
}
